package com.bill.gaty.audrey.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ServiceResponse {
private Integer responseCode;
private String responseMessage;
private String responseType;//FILE ,FILELIST,PRINTERLIST,OKKORESPONSE
private String fileName;//absolute file name  if response data is put into file
private HashMap<String, Object> extraData;
public Boolean isOk(){
	return this.responseCode==200;
}
public Boolean isError(){
	return !this.isOk();
}
public Boolean isFile(){
	return this.responseType.compareTo("FILE")==0;
}
public Boolean isList(){
	return this.isPrinterList()||this.isFileList();
}
public Boolean isPrinterList(){
	return this.responseType.compareTo("PRINTERLIST")==0;
}
public Boolean isFileList(){
	return this.responseType.compareTo("FILELIST")==0;
}
public Boolean isOkKoResponse(){//case of checking existence //no return data
	return this.responseType.compareTo("OKKORESPONSE")==0;
}
 
public Integer getResponseCode() {
	return responseCode;
}
public void setResponseCode(Integer responseCode) {
	this.responseCode = responseCode;
}
public String getResponseMessage() {
	return responseMessage;
}
public void setResponseMessage(String responseMessage) {
	this.responseMessage = responseMessage;
}
public String getResponseType() {
	return responseType;
}
public void setResponseType(String responseType) {
	this.responseType = responseType;
}
public void setResponseType( ) {
	this.responseType = "RAW";
}
public String getFileName() {
	return fileName;
}
public void setFileName(String fileName) {
	this.fileName = fileName;
}
public HashMap<String, Object> getExtraData() {
	return extraData;
}
public void setExtraData(HashMap<String, Object> extraData) {
	this.extraData = extraData;
}
public List<HashMap<String,String>> getPrinterList(){
	return (List<HashMap<String, String>>) this.extraData.get("PRINTERLIST");
}
public List<HashMap<String,String>> getFileList(){
	return (List<HashMap<String, String>>) this.extraData.get("FILELIST");
}
public static ServiceResponse fromRawData(String XMLData){
	System.err.println("XMLData="+XMLData);
	ServiceResponse res=new ServiceResponse();
	Document doc = Jsoup.parse(XMLData);
	String code=doc.getElementsByTag("code").first().text().trim();
	String message=null;
	try{
	message=doc.getElementsByTag("message").first().text();
	}catch(Exception e){
		//e.printStackTrace();
	}
	String type=doc.getElementsByTag("type").first().text().trim();
	
	//res.setResponseCode((code.compareToIgnoreCase("ok")==0?200:500));
	if(code.trim().compareToIgnoreCase("OK")==0)
		res.setResponseCode(200);
	else
		res.setResponseCode(500);
	res.setResponseMessage(message);
	res.setResponseType(type);
	//FILE, LIST,OKKORESPONSE
	if(res.isOkKoResponse()) return res;
	Elements datas = doc.getElementsByTag("datas");
	if(res.isFile()){//send by getFileRequest
		Element element = datas.select("data").first();
		//
		String fileName=element.select("fileName").first().text().trim();
	    String content=element.select("content").first().text().trim();
	    try {
			File f=File.createTempFile(System.currentTimeMillis()+"_", fileName) ;
			FileOutputStream out=new FileOutputStream(f);
			 byte[] decodedContent = org.apache.commons.codec.binary.Base64.decodeBase64(content.getBytes());
		    System.err.println("decoded data length="+decodedContent.length);
		    out.write(decodedContent);
		    out.close();
		    res.setFileName(f.getAbsolutePath());
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	else if(res.isPrinterList()){
		List<HashMap<String,String>> datamap=new ArrayList<HashMap<String,String>>();
		for (Element element : datas.select("data")) {
			//  printerName,owner,user,description
			HashMap<String,String> ext=new HashMap<String, String>();
			ext.put("printerName", element.select("printerName").first().text());
			ext.put("owner", element.select("owner").first().text());
			ext.put("user", element.select("user").first().text());
			ext.put("description", element.select("description").first().text());
			 	datamap.add(ext)   ;
		}
		HashMap<String,Object> extra=new HashMap<String, Object>();
		extra.put("PRINTERLIST", datamap);
		res.setExtraData(extra);
	}
	else{//list Result
		List<HashMap<String,String>> datamap=new ArrayList<HashMap<String,String>>();
		for (Element element : datas.select("data")) {
			//  fileName,size,content,startDate,endDate,sender,requester,description,reference
			HashMap<String,String> ext=new HashMap<String, String>();
			ext.put("fileName", element.select("fileName").first().text());
			ext.put("size", element.select("size").first().text());
			ext.put("content", element.select("content").first().text());
			ext.put("startDate", element.select("startDate").first().text());
			ext.put("endDate", element.select("endDate").first().text());
			ext.put("sender", element.select("sender").first().text());
			ext.put("requester", element.select("requester").first().text());
			ext.put("description", element.select("description").first().text());
			ext.put("reference", element.select("reference").first().text());
			datamap.add(ext)   ;
		}
		HashMap<String,Object> extra=new HashMap<String, Object>();
		extra.put("FILELIST", datamap);
		res.setExtraData(extra);
	}
	
	
	
	return res;
	
}
@Override
public String toString() {
	return "ServiceResponse [responseCode=" + responseCode
			+ ", responseMessage=" + responseMessage + ", responseType="
			+ responseType + ", fileName=" + fileName + ", extraData="
			+ extraData + "]";
}
 
}
